package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.dao.IBaseDateDao;
import com.boss.salary.dao.ISalaryBasedataDao;
import com.boss.salary.entity.BaseData;
import com.boss.salary.entity.PersonTypeSalary;
import com.boss.salary.entity.SalaryBasedata;
import com.boss.salary.service.IBaseDataService;
import com.boss.salary.service.IPersonTypeSalaryService;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class BaseDataServiceImpl implements IBaseDataService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(BaseDataServiceImpl.class);

    @Autowired
    private IBaseDateDao baseDateDao;

    @Autowired
    private IPersonTypeSalaryService personTypeSalaryService;
    @Autowired
    private ISalaryBasedataDao salaryBasedataDao;

    @Override
    public PageDTO<BaseData> querySalaryTypeBaseDataPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) {
        queryParam.put("type","salary_type");
        List<BaseData> list = baseDateDao.queryBaseDataPage(paginationDTO,queryParam);
        int total = baseDateDao.queryBaseDataPageTotil(queryParam);
        PageDTO<BaseData> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    public void saveSalaryTypeBaseData(BaseData baseData, UserDTO userDTO) throws Exception {

        baseData.setMof_div_code(userDTO.getProvince());
        baseData.setType("salary_type");
        int isExist=baseDateDao.selectBaseDataIsExist(baseData);
        if(isExist>0){
            throw new Exception("工资类型code已存在！");
        }
        baseData.setId(SysUtils.generateGuid());
        baseDateDao.saveBaseData(baseData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalaryTypeBaseData(BaseData baseData, UserDTO userDTO) {
        baseData.setMof_div_code(userDTO.getProvince());
        baseData.setType("salary_type");
        baseDateDao.updateBaseData(baseData);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitSalaryTypeBaseData(List<BaseData> basedataList,UserDTO userDTO) throws Exception {
        //获取区划下实发应发工资项，默认挂接用
        List<SalaryBasedata> salaryBasedataList=salaryBasedataDao.getSalaryBaseDataInfoS1S2(userDTO.getProvince());

        for(BaseData baseData : basedataList){
            if(StringUtils.isEmpty(baseData.getId())){
                baseData.setMof_div_code(userDTO.getProvince());
                baseData.setType("salary_type");
                int isExist=baseDateDao.selectBaseDataIsExist(baseData);
                if(isExist>0){
                    throw new Exception(baseData.getCode()+"工资类型code已存在！");
                }
                String uuid=SysUtils.generateGuid();
                baseData.setId(uuid);
                //ID为空即新增
                baseDateDao.saveBaseData(baseData);
                for(SalaryBasedata salaryBasedata:salaryBasedataList){
                    PersonTypeSalary personTypeSalary =new PersonTypeSalary(salaryBasedata,baseData);
                    personTypeSalaryService.savePersonTypeSalary(personTypeSalary,userDTO);
                }
            }else{
                baseData.setMof_div_code(userDTO.getProvince());
                baseData.setType("salary_type");
                //ID存在即编辑
                baseDateDao.updateBaseData(baseData);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalaryType(BaseData baseData, UserDTO userDTO) {
        baseDateDao.deleteSalaryType(baseData,userDTO);
        baseDateDao.personTypeSalary(baseData,userDTO);

    }
}
